package patterns.structural.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合模式示例
 * 将对象组合成树形结构以表示"部分-整体"的层次结构，使得用户对单个对象和组合对象的使用具有一致性
 */
public class CompositePattern {

    public static void main(String[] args) {
        // 文件系统示例
        System.out.println("===== 文件系统示例 =====");
        
        // 创建文件
        FileSystemComponent file1 = new File("文档.txt", 10);
        FileSystemComponent file2 = new File("图片.jpg", 20);
        FileSystemComponent file3 = new File("视频.mp4", 30);
        
        // 创建文件夹并添加文件
        Directory documents = new Directory("文档");
        documents.add(file1);
        
        Directory media = new Directory("媒体");
        media.add(file2);
        media.add(file3);
        
        // 创建根目录并添加文件夹
        Directory root = new Directory("根目录");
        root.add(documents);
        root.add(media);
        
        // 显示整个文件系统
        root.display(0);
        
        // 计算总大小
        System.out.println("总大小: " + root.getSize() + "KB");
        
        // 组织结构示例
        System.out.println("\n===== 组织结构示例 =====");
        
        // 创建员工
        Employee developer1 = new Developer("张三", "前端开发");
        Employee developer2 = new Developer("李四", "后端开发");
        Employee designer = new Designer("王五", "UI设计");
        
        // 创建经理并添加下属
        Manager devManager = new Manager("赵六", "开发经理");
        devManager.addSubordinate(developer1);
        devManager.addSubordinate(developer2);
        
        Manager designManager = new Manager("钱七", "设计经理");
        designManager.addSubordinate(designer);
        
        // 创建总监并添加经理
        Manager director = new Manager("孙八", "技术总监");
        director.addSubordinate(devManager);
        director.addSubordinate(designManager);
        
        // 显示组织结构
        director.showDetails(0);
    }
}

/**
 * 文件系统示例
 */
// 组件接口
interface FileSystemComponent {
    String getName();
    int getSize();
    void display(int indent);
}

// 叶子节点：文件
class File implements FileSystemComponent {
    private String name;
    private int size; // 单位：KB
    
    public File(String name, int size) {
        this.name = name;
        this.size = size;
    }
    
    @Override
    public String getName() {
        return name;
    }
    
    @Override
    public int getSize() {
        return size;
    }
    
    @Override
    public void display(int indent) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < indent; i++) {
            sb.append("  ");
        }
        System.out.println(sb.toString() + "- " + name + " (" + size + "KB)");
    }
}

// 组合节点：目录
class Directory implements FileSystemComponent {
    private String name;
    private List<FileSystemComponent> children = new ArrayList<>();
    
    public Directory(String name) {
        this.name = name;
    }
    
    public void add(FileSystemComponent component) {
        children.add(component);
    }
    
    public void remove(FileSystemComponent component) {
        children.remove(component);
    }
    
    @Override
    public String getName() {
        return name;
    }
    
    @Override
    public int getSize() {
        int totalSize = 0;
        for (FileSystemComponent component : children) {
            totalSize += component.getSize();
        }
        return totalSize;
    }
    
    @Override
    public void display(int indent) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < indent; i++) {
            sb.append("  ");
        }
        System.out.println(sb.toString() + "+ " + name + " (" + getSize() + "KB)");
        
        for (FileSystemComponent component : children) {
            component.display(indent + 1);
        }
    }
}

/**
 * 组织结构示例
 */
// 抽象组件
abstract class Employee {
    protected String name;
    protected String position;
    
    public Employee(String name, String position) {
        this.name = name;
        this.position = position;
    }
    
    public abstract void showDetails(int indent);
}

// 叶子节点：开发人员
class Developer extends Employee {
    public Developer(String name, String position) {
        super(name, position);
    }
    
    @Override
    public void showDetails(int indent) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < indent; i++) {
            sb.append("  ");
        }
        System.out.println(sb.toString() + "- " + name + " (" + position + ")");
    }
}

// 叶子节点：设计师
class Designer extends Employee {
    public Designer(String name, String position) {
        super(name, position);
    }
    
    @Override
    public void showDetails(int indent) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < indent; i++) {
            sb.append("  ");
        }
        System.out.println(sb.toString() + "- " + name + " (" + position + ")");
    }
}

// 组合节点：经理
class Manager extends Employee {
    private List<Employee> subordinates = new ArrayList<>();
    
    public Manager(String name, String position) {
        super(name, position);
    }
    
    public void addSubordinate(Employee employee) {
        subordinates.add(employee);
    }
    
    public void removeSubordinate(Employee employee) {
        subordinates.remove(employee);
    }
    
    @Override
    public void showDetails(int indent) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < indent; i++) {
            sb.append("  ");
        }
        System.out.println(sb.toString() + "+ " + name + " (" + position + ")");
        
        for (Employee employee : subordinates) {
            employee.showDetails(indent + 1);
        }
    }
} 